﻿//using log4net;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Web;


///// <summary>
///// LogHelper 的摘要说明
///// </summary>
//[assembly: log4net.Config.XmlConfigurator(Watch = true, ConfigFile = "log4net.config")]
//namespace Log4net
//{
//    public class Log4netHelper
//    {
//        //private static ILog logger = LogManager.GetLogger("ERROR");
//        /// 记录错误日志
//        /// <summary>
//        /// 记录错误日志
//        /// </summary>
//        /// <param name="error"></param>
//        /// <param name="req"></param>
//        /// <param name="res"></param>
//        /// <param name="extContent"></param>
//        public static void Error(string message, Exception ex)
//        {
//            try
//            {
//                Type type = MethodBase.GetCurrentMethod().DeclaringType;
//                ILog logger = LogManager.GetLogger(type);

//                logger.Error("这是一个Error日志", ex);
//            }
//            catch (Exception err)
//            {
//                throw;
//            }
//        }
//    }
//}

public class LogHelper
{
    /// 异常日志
    /// <summary>
    /// 异常日志
    /// </summary>
    /// <param name="method">方法名</param>
    /// <param name="ex"></param>
    public static void Error(string method, Exception ex)
    {
        try
        {
            errorlog log = new errorlog();
            log.Method = method;
            log.Message = ex.ToString();
            InsertErrorLog(log);
        }
        catch (Exception err)
        {
            throw;
        }
    }

    /// 错误日志
    /// <summary>
    /// 错误日志
    /// </summary>
    /// <param name="method"></param>
    /// <param name="message"></param>
    public static void Error(string method, string message)
    {
        try
        {
            errorlog log = new errorlog();
            log.Method = method;
            log.Message = message;
            InsertErrorLog(log);
        }
        catch (Exception err)
        {
            throw;
        }
    }

    private static bool InsertErrorLog(errorlog log)
    {
        string sqlStr = @"insert into errorlog(CreateTime,Method,Message) 
                          values (now(), ?Method, ?Message)";
        MySqlParameter[] param = new MySqlParameter[] { 
            new MySqlParameter("?Method", log.Method),
            new MySqlParameter("?Message", log.Message)
        };
        return MySQLDataControl.ExecuteNonQuery(MySQLDataControl.GetConnection("MySqlLogConn"), CommandType.Text, sqlStr, param) > 0;
    }
}

public class errorlog
{
    public int Id;
    public DateTime CreateTime;
    public string Method;
    public string Message;
}